#!/bin/sh

. /lib/functions.sh
. /lib/functions/system.sh

sed -i "s/.*linkit$//g" /etc/opkg/distfeeds.conf

BAUDRATE=`uci get yunbridge.config.baudrate`
[ -n "${BAUDRATE}" ] || {
	uci set yunbridge.config.baudrate=2500000
	uci commit yunbridge
}

wan_mac=$(mtd_get_mac_binary factory 4)

uci set network.wan.macaddr=$wan_mac
uci commit network

uci delete uhttpd.main.alias
uci commit uhttpd

WMODE=`uci get  wireless.radio0.linkit_mode`
[ -z "${WMODE}" ] && {
	uci set wireless.radio0.linkit_mode=ap
	uci set wireless.sta.disabled=0
}

[ "$(uci get system.@system[-1].linkit_firstboot)" = "1" ] && return 0

. /lib/ramips.sh
board=$(ramips_board_name)

[ "$board" = "linkits7688d" ] || rm /etc/avahi/services/arduino.service


uci set system.@system[-1].hostname="mylinkit"
uci set wireless.radio0.disabled=0
uci set network.lan.ipaddr=192.168.100.1

SSID=`fw_printenv -n wifi_ssid`
KEY=`fw_printenv -n wifi_key`
SEQ=`fw_printenv -n wifi_seq`
[ -n "${SSID}" ] || { \
	MAC=$(dd bs=1 skip=7 count=3 if=/dev/mtd2 2>/dev/null | hexdump -v -n 3 -e '3/1 "%02X"')
	SSID=LinkIt_Smart_7688_${MAC}
}
[ -n "${SEQ}" ] || \
	SEQ=1
uci set wireless.ap.ssid="$SSID"
uci set wireless.ap.seq="$SEQ"

[ -n "${KEY}" ] && {
	uci set wireless.ap.encryption='psk2'
	uci set wireless.ap.key="$KEY"
}

uci set network.wan=interface
uci set network.wan.proto=dhcp

uci set mountd.mountd.path=/Media/

uci commit
return 0
